/*
** sdl_tools.c for project in /u/all/muller_b/cu/travail/c/
**
** Made by quentin muller
** Login   <muller_b@epitech.net>
**
** Started on  Tue Apr  6 10:52:08 2010 quentin muller
** Last update Tue Apr 27 21:44:51 2010 leroy_d
*/

#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <time.h>

#include "Game.h"

void			Game::cls()
{
  SDL_FillRect(SDL_GetVideoSurface(), NULL, SDL_MapRGB(SDL_GetVideoSurface()->format, 0, 0, 0));
}

bool			Game::init_SDL()
{
  if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
    {
      std::cout << "Error SDL : Cant initialize SDL.\n" << std::endl;
      return false;
    }
  if ((this->_screen = SDL_SetVideoMode(_xdim, _ydim, _bpp, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL)
    {
      std::cout << "Error SDL : Cant set video mode.\n" << std::endl;
      return false;
    }
  SDL_WM_SetCaption("LoLPoW", NULL);
  if (TTF_Init() < 0)
    {
      std::cerr << "TTF error : %s\n" << TTF_GetError() << std::endl;
      return false;
    }
  this->font_load();
  this->ini_keys();
  return true;
}

int			Game::timer_countdown(int *start, int seconds)
{
  if ((((int)clock() / 10000) - *start) > seconds)
    {
      *start = 0;
      return (0);
    }
  return (1);
}

int			Game::get_timer()
{
  return (((int)clock() / 10000));
}

void			Game::wait()
{
  char			c;

  std::cout << "Push enter to continue..." << std::endl;
  std::cin >> c;
}
void			Game::dot32(Uint16 x, Uint16 y, Uint32 color)
{
  Uint8 * p = ((Uint8 *)this->_screen->pixels) + y * this->_screen->pitch + x * (_bpp / 8);
  *(Uint32 *)p = color;
}

void			Game::dot32(SDL_Surface* dst, Uint16 x, Uint16 y, Uint32 color)
{
  Uint8 * p = ((Uint8 *)dst->pixels) + y * dst->pitch + x * (_bpp / 8);
  *(Uint32 *)p = color;
}

Uint32			Game::getDot32(SDL_Surface* dst, Uint16 x, Uint16 y)
{
  Uint8 * p = ((Uint8 *)dst->pixels) + y * dst->pitch + x * (_bpp / 8);
  return *(Uint32*)p;
}

void			Game::maxDot32(SDL_Surface* dst, Uint16 x, Uint16 y, Uint32 color)
{
  Uint8 * p = ((Uint8 *)dst->pixels) + y * dst->pitch + x * (_bpp / 8);
  Uint8 * pc = ((Uint8 *)&color);
  if (*p < *pc)
    *p = *pc;
  ++pc; ++p;
  if (*p < *pc)
    *p = *pc;
  ++pc; ++p;
  if (*p < *pc)
    *p = *pc;
  ++pc; ++p;
  if (*p < *pc)
    *p = *pc;
}

void			Game::dot(Uint16 x, Uint16 y, Uint32 color)
{
  Uint8 * p = ((Uint8 *)this->_screen->pixels) + y * this->_screen->pitch + x * (_bpp / 8);

  switch(_bpp / 8)
    {
    case 1:
      *p = (Uint8) color;
      break;
    case 2:
      *(Uint16 *)p = (Uint16) color;
      break;
    case 3:
      if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
	{
	  *(Uint16 *)p = ((color >> 8) & 0xff00) | ((color >> 8) & 0xff);
	  *(p + 2) = color & 0xff;
	}
      else
	{
	  *(Uint16 *)p = color & 0xffff;
	  *(p + 2) = ((color >> 16) & 0xff) ;
	}
      break;
    case 4:
      *(Uint32 *)p = color;
      break;
    }
}
